﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArithmeticDemo
{
    public class LeeCode704
    {
        public void Start() {
            int[] nums = { -1, 0, 3, 5, 9, 12 };
            //var target = 9;
            //var result = Search(nums, target);
            Console.WriteLine(Search(nums, 9));
            Console.WriteLine(Search(nums, 2));
            Console.WriteLine(Search(nums, 13));
        }

        public int Search(int[] nums, int target) {
            var result = -1;
            var left = 0;
            var right = nums.Length-1;
            int i = 0;
            while (left <= right) {
                i = left + (right - left) / 2;
                if (target == nums[i]) {
                    result = i ;
                    break;
                }
                if (target > nums[i]) {
                    left = i+1;
                } else if (target < nums[i]) {
                    right = i-1;
                }
                
            }
            return result;
        }
    }
}
